﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_WareHouseManage_frm_ProductRemove : System.Web.UI.Page
{
    private static string strType = "08";//移库单

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                }
            }
        }
    }

    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetProductRemoveByMain();
            }
            else
            {
                this.labMessage.Text = "";
                this.lblJs.Text = "";//清空Js脚本
            }
        }
        else
        {
            //ClsMenuDAL cls_Menu = new ClsMenuDAL();
            //string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    /// <summary>
    /// 主表记录翻页
    /// </summary>
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindProductRemoveByDataView(dst, intPageIndex);
        }
    }

    /// <summary>
    /// 从表记录翻页
    /// </summary>
    public void ChildChangPage(object sender, EventArgs e)
    {
        //获取保存的从表中的结果集和DataGrid控件
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;
        DataGrid dgrd = cls_Property.SetOrGetChildDataGridControl;

        if (dst != null && dgrd != null)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = dgrd.CurrentPageIndex;
            int intPageCounts = dgrd.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindProductRemoveChildByDataView(dgrd, dst, intPageIndex);
        }
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    public void AddProductRemoveInfo(object sender, EventArgs e)
    {
        try
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            ClsRemoveProductDAL clsRemoveProduct = new ClsRemoveProductDAL();
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            ImageButton ibtnAdd = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnAdd.CommandName);

            string strUserId = cls_Property.SetOrGetUserID;

            if (intCommandName == 0)//打开主表添加面板
            {               
                //获取系统编码
                string strCode = cls_Menu.GetSystemCode("0", "00", "", 60);
                if (strCode != "Error")
                {
                    this.panAddResult.Visible = true;
                    this.ctbAddtm_no.Text = strCode;
                    this.ctsbAddtm_date.Text = System.DateTime.Today.ToShortDateString();
                    this.ctbAddoper.Text = strUserId;
                    this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAddtm_date.ClientID + "_txtBox').focus();</script>";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                }  
            }
            else if (intCommandName == 1)//关闭主表添加面板
            {
                this.panAddResult.Visible = false;
            }
            else if (intCommandName == 2)//保存主表添加记录
            {
                //保存主表新增记录的代码
                string strTm_no = this.ctbAddtm_no.Text.Trim();//调拔单号
                DateTime dtTm_date = DateTime.Parse(this.ctsbAddtm_date.Text.Trim());//调拔日期
                string strWho_id = this.ctsbAddwho_id.Text.Trim();//出库仓号
                string strWhi_id = this.ctsbAddwhi_id.Text.Trim();//入库仓号                
                string strRemark = this.ctbremark.Text.Trim();//备注

                Boolean bolIsCussee = clsRemoveProduct.AddRemoveProduct(strTm_no, dtTm_date, "", "", "",
                    strWho_id, strWhi_id, "", strUserId, strRemark);

                if (bolIsCussee)
                {
                    //重新绑定结果
                    this.GetProductRemoveByMain();
                   
                    //主表新增后直接打开从表进行新增从表记录 Begin///////////////////////////
                    this.panAddResult.Visible = false;
                    ImageButton ibtn = new ImageButton();
                    ibtn.CommandName = "0";

                    int intItems = this.dgrdResult.Items.Count - 1;

                    for (int i = intItems; i >= 0; i--)
                    {
                        string strRs_DmNO = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no")).Text.Trim();

                        if (strRs_DmNO == strTm_no)
                        {
                            ibtn.CommandArgument = i.ToString();
                            break;
                        }
                    }
                    this.OpenChildResult(ibtn, null);
                    this.AddProductRemoveInfo(this.ibtnAdd, null);
                    //主表新增后直接打开从表进行新增从表记录 End///////////////////////////
                }
                else
                {
                    Response.Write("<script>alert('新增失败！');</script>");
                }
            }
            else
            {
                int intIndex = Int32.Parse(this.ibtnAdd.CommandArgument);

                Panel panChildAddResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panChildAddResult");

                if (intCommandName == 3)//关闭从表添加面板
                {
                    panChildAddResult.Visible = false;
                }
                else if (intCommandName == 4)//保存从表添加记录----------------------------
                {
                    //此处写保存从表新增记录的代码
                    DataGrid dgrd = new DataGrid();
                    string strTm_No = "";//主表单据号
                    int intChildCount = this.dgrdResult.Items.Count;

                    for (int i = 0; i < intChildCount; i++)
                    {
                        Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                        if (panItem.Visible)
                        {
                            strTm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no")).Text.Trim();//主表单据号
                            dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                            break;
                        }
                    }
                    string strProductId = ((CuTextSelectBox)dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0")).Text.Trim();//商品编号
                    int iChildAcount = int.Parse(((CuTextBox)dgrdResult.Items[intIndex].FindControl("ctbChildAdd3")).Text.Trim());////数量
                    //decimal dChildPrice = decimal.Parse(((CuTextBox)dgrdResult.Items[intIndex].FindControl("ctbChildAdd5")).Text.Trim());//单价
                    string strChildRemark = ((CuTextBox)dgrdResult.Items[intIndex].FindControl("ctbChildAdd8")).Text.Trim();//备注

                    CuTextBox ctbChildAddBatchNo = (CuTextBox)dgrdResult.Items[intIndex].FindControl("ctbChildAddBatchNo");//批号
                    CuTextBox ctbChildAddIdentifyNo = (CuTextBox)dgrdResult.Items[intIndex].FindControl("ctbChildAddIdentifyNo");//识别码

                    CuTextBox is_Start = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("is_Start");
                    Boolean bStart = Boolean.Parse(((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("is_Start")).Text.Trim());//是否启用批号
                    string str_BatchNo = ctbChildAddBatchNo.Text.Trim();
                    if (bStart)//如果启用批号，则必须需入批号。否则不允许保存
                    {
                        if (str_BatchNo.Length == 0)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('此商品批号不能为空！');</script>！";
                            //this.lblJs.Text = "<script>document.getElementById('" + is_Start.ClientID + "_txtBox').focus();</script>";
                        }
                        else
                        {
                            Boolean bolIsCussee = clsRemoveProduct.AddChildRemoveProduct(strTm_No, strProductId, iChildAcount,
                                 0, ctbChildAddBatchNo.Text.Trim(), ctbChildAddIdentifyNo.Text.Trim(), strChildRemark);
                            if (!bolIsCussee)
                            {
                                this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增明细失败，请重试！')</script>");
                            }
                            //新增后重新绑定明细表
                            DataSet dst = clsRemoveProduct.GetRemoveProductChilds(strTm_No);

                            if (dst != null && dst.Tables[0].Rows.Count > 0)
                            {
                                this.BindProductRemoveChildByDataView(dgrd, dst, 0);
                            }
                        }
                    }
                    else
                    {
                        Boolean bolIsCussee = clsRemoveProduct.AddChildRemoveProduct(strTm_No, strProductId, iChildAcount,
                                     0, ctbChildAddBatchNo.Text.Trim(), ctbChildAddIdentifyNo.Text.Trim(), strChildRemark);
                        if (!bolIsCussee)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增明细失败，请重试！')</script>");
                        }
                        //新增后重新绑定明细表
                        DataSet dst = clsRemoveProduct.GetRemoveProductChilds(strTm_No);

                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                        {
                            this.BindProductRemoveChildByDataView(dgrd, dst, 0);
                        }
                    }

                }
                else if (intCommandName == 5)//打开从表添加面板
                {
                    panChildAddResult.Visible = true;
                    CuTextSelectBox ctsbChildAdd0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbChildAdd0.ClientID + "_txtBox').focus();</script>";
                }
            }
        }
        catch(Exception ex)
        {
            //Response.Write(ex.Message);            
            this.labMessage.Text = "<script language='javascript'>alert('请输入正确的格式和必填项！');</script>！";
        }
    }

    /// <summary>
    /// 修改记录
    /// </summary>
    public void UpDateProductRemoveInfo(object sender, EventArgs e)
    {
        try
        {
            ClsRemoveProductDAL clsRemoveProduct = new ClsRemoveProductDAL();
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            ImageButton ibtnUpdate = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnUpdate.CommandName);
            int intIndex = Int32.Parse(ibtnUpdate.CommandArgument);

            string strUserID = cls_Property.SetOrGetUserID;

            if (intCommandName == 0)//主表确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect");
                    if (chkBox.Checked)
                    {
                        //此处写确认的代码
                        CuTextBox txtBox = (CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no");//主表ID号                                                                       
                        string strReturn = clsRemoveProduct.CheckWhetherAudtitRemove(txtBox.Text.Trim(), strType, 0, strUserID);
                        string[] arrReValue = strReturn.Split(new char[] { '|' });
                        if (arrReValue[0] == "N")//不可以取消确认，得到返回的批号
                        {
                            if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号未入库,请确认！');</script>";
                            }
                            else
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品未入库,请确认！');</script>";
                            }
                        }
                        else if (arrReValue[0] == "Q")
                        {
                            if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号库存不足,请调整！');</script>";
                            }
                            else
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品库存不足,请确认！');</script>";
                            }

                        }
                        else
                        {
                            if (strReturn != "0")
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('确认失败！');</script>";
                            }
                        }                                                  
                    }
                }
                //重新绑定结果
                this.GetProductRemoveByMain();
            }
            else if (intCommandName == 3)//主表取消确认
            {
                //取消确认的代码
                int iIndex = int.Parse(ibtnUpdate.CommandArgument);
                CuTextBox txtBox = (CuTextBox)this.dgrdResult.Items[iIndex].FindControl("ctbRstm_no");//主表ID号
                string strReturn = clsRemoveProduct.CheckWhetherAudtitRemove(txtBox.Text.Trim(), strType, 1, strUserID);
                string[] arrReValue = strReturn.Split(new char[] { '|' });
                if (arrReValue[0] == "N")//不可以取消确认，得到返回的批号
                {
                    if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号未入库,请确认！');</script>";
                    }
                    else
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品未入库,请确认！');</script>";
                    }
                }
                else if (arrReValue[0] == "Q")
                {
                    if (arrReValue[1].Length > 0 && arrReValue[2].Length > 0)
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品" + arrReValue[2] + "批号库存不足,请调整！');</script>";
                    }
                    else
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('当前" + arrReValue[1] + "商品库存不足,请确认！');</script>";
                    }

                }
                else
                {
                    if (strReturn != "0")
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('取消确认失败！');</script>";
                    }
                }         
                //重新绑定结果
                this.GetProductRemoveByMain();
            }
            else if (intCommandName == 1 || intCommandName == 2 || intCommandName == 4)//主表编辑
            {
                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
                ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");

                CuTextBox ctbRstm_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRstm_no");//调拔单号
                CuTextSelectBox ctsbRstm_date = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRstm_date");//调拔日期                
                CuTextSelectBox ctsbRswho_id = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRswho_id");//出库仓号
                CuTextSelectBox ctsbRswhi_id = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRswhi_id");//入库仓号                
                CuTextBox ctsbRsremark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsremark");//备注

                if (intCommandName == 1)//取消编辑
                {
                    chkRsSelect.Visible = ibtnRsEdit.Visible = true;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = ibtnRsOAudit.Visible = false;
                    ctsbRstm_date.ReadOnly = true;
                    ctsbRswho_id.ReadOnly = ctsbRswhi_id.ReadOnly = ctsbRsremark.ReadOnly = true;
                }
                else if (intCommandName == 2)//编辑
                {
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    chkRsSelect.Visible = ibtnRsEdit.Visible = ibtnRsOAudit.Visible = false;
                    ctsbRstm_date.ReadOnly = false;
                    ctsbRswho_id.ReadOnly = ctsbRswhi_id.ReadOnly = ctsbRsremark.ReadOnly = false;
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbRstm_date.ClientID + "_txtBox').select();document.getElementById('" + ctsbRstm_date.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    chkRsSelect.Visible = ibtnRsEdit.Visible = true;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = ibtnRsOAudit.Visible = false;
                    ctsbRstm_date.ReadOnly = true;
                    ctsbRswho_id.ReadOnly = ctsbRswhi_id.ReadOnly = ctsbRsremark.ReadOnly = true;

                    //此处写保存编辑的代码
                    Boolean bolIsCussee = clsRemoveProduct.UpDateRemoveProduct(ctbRstm_no.Text.Trim(), DateTime.Parse(ctsbRstm_date.Text.Trim()),
                        "", "", "", ctsbRswho_id.Text.Trim(), ctsbRswhi_id.Text.Trim(),
                        "", ctsbRsremark.Text.Trim());

                    if (!bolIsCussee)
                    {
                        Response.Write("<script>alert('修改失败！');</script>");
                    }

                    //重新绑定结果
                    this.GetProductRemoveByMain();
                }
            }
            else//从表相关操作
            {
                //从表修改记录的代码
                DataGrid dgrd = new DataGrid();
                string strTm_No = "";//主表单据号
                int intChildCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intChildCount; i++)
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        strTm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no")).Text.Trim();//主表单据号
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        break;
                    }
                }

                CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[intIndex].FindControl("chkChildRsSelect");
                ImageButton ibtnChildRsCancel = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsCancel");
                ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsEdit");
                ImageButton ibtnChildRsServer = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsServer");

                CuTextSelectBox ctsbProductId = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctsbChildRs0");//商品编号
                CuTextBox ctbQry = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs3");//数量                               
                CuTextBox ctbChildRsBatchNo = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsBatchNo");//批号
                //CuTextBox ctbChildRsIdentifyNo = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsIdentifyNo");//识别码
                CuTextBox ctbRemark = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs8");//备注

                if (intCommandName == 6)//从表编辑
                {
                    chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = false;
                    ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = true;
                    ctbChildRsBatchNo.ReadOnly = false; 
                    ctbQry.ReadOnly = ctbRemark.ReadOnly = false;
                }
                else if (intCommandName == 5)//从表取消编辑
                {
                    chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = true;
                    ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = false;
                    ctbQry.ReadOnly = ctbRemark.ReadOnly = true;
                }
                else if (intCommandName == 7)//从表保存编辑
                {
                    int iId = int.Parse(dgrd.Items[intIndex].Cells[1].Text.Trim());
                    Boolean bolIsCussee = clsRemoveProduct.UpDateRemoveProductD(iId, strTm_No, ctsbProductId.Text.Trim(), decimal.Parse(ctbQry.Text.Trim()),
                        0, ctbChildRsBatchNo.Text.Trim(), "", ctbRemark.Text.Trim());
                    if (!bolIsCussee)
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增明细失败，请重试！')</script>");
                    }
                    else
                    {
                        //修改后重新绑定明细表
                        DataSet dst = clsRemoveProduct.GetRemoveProductChilds(strTm_No);

                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                        {
                            this.BindProductRemoveChildByDataView(dgrd, dst, 0);
                        }
                    }
                }
            }
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('请输入正确的格式和必填项！');</script>");
        }
    }

    /// <summary>
    /// 删除记录
    /// </summary>
    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteProductRemoveInfo(intCommandName);
    }

    /// <summary>
    /// 展开或收缩从表信息
    /// </summary>
    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnOpen = (ImageButton)sender;
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);
        int intCount = this.dgrdResult.Items.Count;
        bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text);

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");

        //显示从表信息
        CuTextBox ctbRstm_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRstm_no");
        ClsRemoveProductDAL clsRemoveProduct = new ClsRemoveProductDAL();
        DataSet dst = clsRemoveProduct.GetRemoveProductChilds(ctbRstm_no.Text.Trim());

        if (dst == null || dst.Tables[0].Rows.Count <= 0)
        {
            ibtnOpen.Attributes.Add("OnClick", "return confirm('单据录入未完成，您要删除此单据吗？');");
        }

        if (intCommandName == 0)//展开
        {
            for (int i = 0; i < intCount; i++)
            {
                //找到展开的项
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (i == intIndex)
                {
                    panItem.Visible = true;
                }
                else
                {
                    panItem.Visible = false;
                }
            }

            //主表展开后则不可操作
            this.ibtnAdd.CommandName = "5";
            this.ibtnRef.CommandName = "1";
            this.ibtnDelete0.CommandName = "-1";//删除从表用（假删除）
            this.lbtnDelete0.CommandName = "-2";//删除从表用（假删除）
            this.lbtnDelete1.CommandName = "-3";//删除从表用（真删除）
            this.ibtnAdd.CommandArgument = intIndex.ToString();
            panShowChildResult.Visible = true;
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            //如果主表确认后则从表不可操作
            if (is_audit)
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";
                this.imgDelete.Attributes.Add("OnMouseOver", "");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }

            ////显示从表信息
            //CuTextBox ctbRstm_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRstm_no");
            //ClsRemoveProductDAL clsRemoveProduct = new ClsRemoveProductDAL();
            //DataSet dst = clsRemoveProduct.GetRemoveProductChilds(ctbRstm_no.Text.Trim());

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");
                //绑定从表信息
                this.BindProductRemoveChildByDataView(dgrd, dst, 0);
            }
        }
        else//收缩
        {
            //收缩后都可操作
            this.ibtnAdd.CommandName = "0";
            this.ibtnRef.CommandName = "0";
            this.ibtnDelete0.CommandName = "0";//恢复删除主表用（真）
            this.lbtnDelete0.CommandName = "0";//恢复删除主表用（假）
            this.lbtnDelete1.CommandName = "1";//恢复删除主表用（假）
            panShowChildResult.Visible = false;
            ibtnOpenChild.CommandName = "0";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
            this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
            this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
            this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
            this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
            this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
            this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
            this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;

            //重新绑定
            //ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            //DataSet dst = cls_Property.SetOrGetCorporationDataSet;
            //this.BindProductRemoveByDataView(dst, this.dgrdResult.CurrentPageIndex);
            if (dst == null || dst.Tables[0].Rows.Count <= 0)
            {
                Boolean bolIsCussee = clsRemoveProduct.DeleteRemoveProduct(ctbRstm_no.Text.Trim(), intCommandName);
            }

            this.GetProductRemoveByMain();
        }
    }

    /// <summary>
    /// 获取所有主表记录
    /// </summary>
    private void GetProductRemoveByMain()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsRemoveProductDAL ClsRemoveProductDAL = new ClsRemoveProductDAL();

            DataSet dst = ClsRemoveProductDAL.GetRemoveProductMains(strUserID);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindProductRemoveByDataView(dst, 0);
            }
            else
            {
                this.dgrdResult.CurrentPageIndex = 0;
                this.dgrdResult.DataSource = null;
                this.dgrdResult.DataBind();
                this.lblRsCounts.Text = "共0条记录";
                this.lblPageCounts.Text = "共0页";
                this.lblCurrPage.Text = "当前第0页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    /// <summary>
    /// 绑定主表信息到数据控件
    /// </summary>
    private void BindProductRemoveByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
        else
        {
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
        }
    }

    /// <summary>
    /// 绑定从表信息到数据控件
    /// </summary>
    private void BindProductRemoveChildByDataView(DataGrid dgrd, DataSet dst, int CurrPageIndex)
    {
        if (dgrd != null && dst != null)
        {
            dgrd.CurrentPageIndex = CurrPageIndex;
            dgrd.DataSource = dst;
            dgrd.DataBind();

            int intIndex = Int32.Parse(dgrd.ToolTip);
            int intItems = dgrd.Items.Count;

            Label lblChildRsCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildRsCounts");
            Label lblChildPageCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildPageCounts");
            Label lblChildCurrPage = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildCurrPage");
            LinkButton lbtnChildFirst = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildFirst");
            LinkButton lbtnChildPrev = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildPrev");
            LinkButton lbtnChildNext = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildNext");
            LinkButton lbtnChildEnd = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildEnd");

            lblChildRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条明细记录";
            lblChildPageCounts.Text = "共" + dgrd.PageCount + "页";
            lblChildCurrPage.Text = "当前第" + (dgrd.CurrentPageIndex + 1) + "页";
            lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = true;
            lblChildRsCounts.Enabled = lblChildPageCounts.Enabled = lblChildCurrPage.Enabled = true;

            if (dgrd.PageCount == 1)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == dgrd.PageCount - 1)
            {
                lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == 0)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = false;
            }

            //判断从表中的记录是否可操作
            //for (int i = 0; i < intItems; i++)
            //{
            //    CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[i].FindControl("chkChildRsSelect");
            //    ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[i].FindControl("ibtnChildRsEdit");

            //    if (!this.ibtnAdd.Enabled)
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            //    }
            //    else
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = true;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            //    }
            //}

            //保存从表中的结果集和DataGrid控件用于从表中的翻页
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;
            cls_Property.SetOrGetChildDataGridControl = dgrd;
        }
    }

    /// <summary>
    /// 删除记录
    /// </summary>
    private void DeleteProductRemoveInfo(int CommandName)
    {
        ClsRemoveProductDAL clsRemoveProduct = new ClsRemoveProductDAL();
        int intCount = this.dgrdResult.Items.Count;

        if (CommandName == 0 || CommandName == 1)//删除主表(0:假删除,1:真删除)
        {
            for (int i = 0; i < intCount; i++)
            {
                CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect");

                if (chkBox.Checked)
                {
                    CuTextBox txtBox = (CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no");//主表ID号
                    Boolean bolIsCussee = clsRemoveProduct.DeleteRemoveProduct(txtBox.Text.Trim(), CommandName);

                    if (!bolIsCussee)
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除产品调拔信息失败，请重试！')</script>");
                    }
                }
            }

            //重新绑定结果
            this.GetProductRemoveByMain();
        }
        else if (CommandName == -1 || CommandName == -2 || CommandName == -3)//删除从表:-1,-2假删除,-3真删除，
        {
            DataGrid dgrd = new DataGrid();
            string strTm_No = "";//主表单据号
            int intChildCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intChildCount; i++)
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (panItem.Visible)
                {
                    strTm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRstm_no")).Text.Trim();
                    dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                    break;
                }
            }

            int intChildItem = dgrd.Items.Count;

            for (int j = 0; j < intChildItem; j++)
            {
                CheckBox checkBox = (CheckBox)dgrd.Items[j].FindControl("chkChildRsSelect");

                if (checkBox.Checked)
                {
                    int iId = int.Parse(dgrd.Items[j].Cells[1].Text.Trim());
                    string strChildDm_No = ((CuTextSelectBox)dgrd.Items[j].FindControl("ctsbChildRs0")).Text.Trim();//明细产品编号

                    Boolean bolIsCussee = clsRemoveProduct.DeleteChildRemoveProduct(iId,strTm_No, strChildDm_No, CommandName);

                    if (!bolIsCussee)
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除明细失败，请重试！')</script>");
                    }
                }
            }
            
            DataSet dst = clsRemoveProduct.GetRemoveProductChilds(strTm_No);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.BindProductRemoveChildByDataView(dgrd, dst, 0);
            }
            else
            {
                dgrd.DataSource = null;
                dgrd.DataBind();
            }
        }
    }

    /// <summary>
    /// 删除记录事件
    /// </summary>
    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteProductRemoveInfo(intCommandName);
    }

    /// <summary>
    /// 记录页面跳转
    /// </summary>
    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindProductRemoveByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入正确的页码数！')</script>");
                    }
                }
                catch
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入数字类型的页码！')</script>");
                }
            }
            else
            {
                this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入要跳转的页码！')</script>");
            }
        }
    }

    /// <summary>
    /// 主表或从表刷新
    /// </summary>
    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetProductRemoveByMain();
        }
        else//刷新从表
        {

        }
    }

    /// <summary>
    /// 数据控件绑定数据项后被触发的事件
    /// </summary>
    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool Is_audit = bool.Parse(e.Item.Cells[1].Text.Trim());
            bool Is_visible = bool.Parse(e.Item.Cells[2].Text.Trim());//是否作废

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            CuTextBox ctbRs1 = (CuTextBox)e.Item.FindControl("ctbRs1");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");
            Image imgAudit = (Image)e.Item.FindControl("is_audit");

            //修改按回车提交
            //if (ctbRs1.ReadOnly)
            //{
            //    ctbRs1.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            //}

            if (Is_visible)//是否作废
            {
                chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            if (!Is_audit)
            {
                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkRsSelect.Visible = true;
                    }

                    ibtnRsEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkRsSelect.Visible = true;
                }                
            }
            else
            {
                ibtnRsOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }               
            }
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            DataGrid dgrdChildResult = (DataGrid)e.Item.FindControl("dgrdChildResult");
            //子DataGrid注册事件
            dgrdChildResult.ItemDataBound += new DataGridItemEventHandler(dgrdChildResult_ItemDataBound);
        }
    }

    protected void dgrdChildResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            int iIndex = int.Parse(((DataGrid)sender).ToolTip);
            bool Is_audit = bool.Parse(this.dgrdResult.Items[iIndex].Cells[1].Text.Trim());//是否确认
            bool Is_visible = bool.Parse(this.dgrdResult.Items[iIndex].Cells[2].Text.Trim());//是否作废
            CheckBox chkChildRsSelect = (CheckBox)e.Item.FindControl("chkChildRsSelect");
            ImageButton ibtnChildRsEdit = (ImageButton)e.Item.FindControl("ibtnChildRsEdit");
            if (Is_audit || Is_visible)//是否作废
            {
                chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
                ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }
        }
    }

    //根据订单状态进行相关查询
    public void lbtnQuery_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.QueryOrderByStatu(intCommandName);
    }

    private void QueryOrderByStatu(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsRemoveProductDAL cls_RemoveProduct = new ClsRemoveProductDAL();
            DataSet dst = cls_RemoveProduct.GetRemoveByStatu(strUserID, "tbhTranM", int.Parse(strType), CommandName);

            cls_Property.SetOrGetCorporationDataSet = dst;
            this.BindProductRemoveByDataView(dst, 0);
        }
    }   
}

